package com.bankLogisticsSys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bankLogisticsSys.interceptor.ManageLoginInterceptor;
import com.bankLogisticsSys.model.Business;
import com.bankLogisticsSys.model.Networkinfo;
import com.bankLogisticsSys.model.Packageinfo;
import com.bankLogisticsSys.service.PackageService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
@Before(ManageLoginInterceptor.class)
public class ManagePackageController extends Controller {
	PackageService service = new PackageService();
	protected int page=1;
	protected int rows=20;
	protected long totalCount;
	
	public void packageinfoList(){
		page = getParaToInt("page", 1);
		rows = getParaToInt("rows", 20);
		String sort = getPara("sort","id");
		String order = getPara("order","asc");
		
		List<Record> list = service.getPackageList(getPara("network"),page, rows, sort, order);
		totalCount = service.getTotalCount(getPara("network"));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", totalCount);
		map.put("rows", list);
		renderJson(map);
	}
	
	public void networkList(){
		List<Networkinfo> list = service.getNetworkList();
		renderJson(list);
	}
	
	public void businessList(){
		List<Business> list = service.getBusinessList();
		renderJson(list);
	}

	public void list(){
		render("/WEB-INF/content/package/list.html");
	}
	public void add(){
		render("/WEB-INF/content/package/add.html");
	}
	public void modify(){
		int id = getParaToInt("packageinfo.id", 1);
		Packageinfo pk = service.getPackage(id);
		setAttr("packageinfo", pk);
		render("/WEB-INF/content/package/modify.html");
	}
	
	public void insert(){
		Packageinfo pk = getModel(Packageinfo.class);
		if(!service.isExistPackageCode(pk.getPackageCode())){
			pk.setState(0);
			service.insert(pk);
			render("/WEB-INF/content/package/list.html");
		}else{
			setAttr("errMsg", "包裹编号已存在");
			render("/WEB-INF/content/user/add.html");
		}
	}
	
	public void update(){
		Packageinfo pk = getModel(Packageinfo.class);
		service.edite(pk);
		render("/WEB-INF/content/package/list.html");
	}
	
	public void delete(){
		Packageinfo pk = getModel(Packageinfo.class);
		Map<String, Integer> map = new HashMap<String, Integer>();
		if(service.delete(pk)){
			map.put("result", 0);
		}else{
			map.put("result", 1);
		}
		renderJson(map);
	}
}
